﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class Notification

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property

    Private _roleList As List(Of Role)
    Public Property Roles As List(Of Role)
        Get
            If _roleList Is Nothing Then

                _roleList = New List(Of Role)

                For Each roleItem In (From ntr In Database.Instance.NotificationRoles Where ntr.NotificationTypeID = Me.ID Select ntr.Role)
                    _roleList.Add(roleItem)
                Next

            End If
            Return _roleList
        End Get
        Set(ByVal value As List(Of Role))
            _roleList = value
        End Set
    End Property

#End Region

    Public Shared Function RetrieveAll() As IEnumerable(Of Notification)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.Notifications.OrderBy(Function(nt) nt.Description)
    End Function


    Public Shared Function RetrieveAllByRoleID(ByVal roleID As Integer) As IEnumerable(Of Notification)
        Dim db As EquipmentDataContext = Database.Instance
        Return From n In Database.Instance.Notifications Where n.NotificationRoles.Select(Function(r) r.UserRoleID).Contains(roleID) Order By n.Description
    End Function

    Public Shared Function RetrieveAllByTrigger(ByVal trigger As NotificationTrigger) As IEnumerable(Of Notification)
        Dim db As EquipmentDataContext = Database.Instance
        Return From n In Database.Instance.Notifications Where n.NotificationTrigger = trigger Order By n.Description
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As Notification
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From nt In db.Notifications Where nt.ID = id
        Dim notificationTypeEntity As Notification = result.FirstOrDefault()
        notificationTypeEntity.DBContext = db
        Return notificationTypeEntity
    End Function

    Public Sub MarkAsCompletedForWorkOrder(ByVal wo As WorkOrder)
        Dim db As EquipmentDataContext = Database.Instance
        Dim newCompletedEntity As New WOCompletedNotification()
        newCompletedEntity.NotificationID = Me.ID
        newCompletedEntity.WorkOrderID = wo.ID
        newCompletedEntity.CompletedDate = DateTime.Now.ToUniversalTime()
        db.WOCompletedNotifications.InsertOnSubmit(newCompletedEntity)
        Try
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
    End Sub


    Public Shared Sub Insert(ByVal entity As Notification)
        Dim db As EquipmentDataContext = Database.Instance
        db.Notifications.InsertOnSubmit(entity)
        Try
            db.SubmitChanges()
            For Each role In entity.Roles
                Dim newItem = New NotificationRole()
                newItem.NotificationTypeID = entity.ID
                newItem.UserRoleID = role.ID
                db.NotificationRoles.InsertOnSubmit(newItem)
            Next
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try
    End Sub

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext
            Dim roles = From ntr In _dataBase.NotificationRoles Where ntr.NotificationTypeID = Me.ID Select ntr
            Try
                For Each role In roles
                    Dim currRole As Role = Me.Roles.SingleOrDefault(Function(r) r.ID = role.UserRoleID)
                    If currRole Is Nothing Then
                        _dataBase.NotificationRoles.DeleteOnSubmit(role)
                    End If
                Next
                For Each role In Me.Roles
                    Dim currRole As NotificationRole = roles.SingleOrDefault(Function(r) r.UserRoleID = role.ID)
                    If currRole Is Nothing Then
                        currRole = New NotificationRole() With {.UserRoleID = role.ID, .NotificationTypeID = Me.ID}
                        _dataBase.NotificationRoles.InsertOnSubmit(currRole)
                    End If
                Next
                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If

    End Sub

End Class
